<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <#include "/pubHeadStyleTree.html">
        <script src="${ctx}/formStatic/verify.js"></script>
        <script src="${ctx}/formStatic/form/form.js"></script>
        <script src="${ctx}/formStatic/form/CommonDialog.js" type="text/javascript"></script>
        <style>
            .temTable td{
                padding: 10px;
            }
            .uploadImgCont,.uploadFileCont{
                text-align: center;
            }
            .subOptState{
                width: 200px;
                text-align: center;
            }
            .addFormTr {
                margin-bottom: 10px;
            }
            .temTag{
                text-align: center;
            }
        </style>
        </head>
<body>
<div class="layui-form" style="height:88%;overflow: auto" lay-filter="formData" id="formDefEdit">
    <div class="layui-btn-container" style="position: absolute;left: 46%;bottom: 0;">
        <button class="layui-btn layui-btn-danger return">取消</button>
    </div>
    <input type="hidden" id="mainTableId" name="mainTableId" value="${id}"/>
    <div class="formPreview templateAddEdit">
        <div class="temCont">
            <div class="dragGather" request="template/table">
                <div class="" style="padding: 10px">
                    <table class="temTable undefined tableExcel" border="1" width="100%" cellpadding="10" cellspacing="10">
                        <tbody request="template/table">
                        <tr class="temSpace" request="template/table">
                            <td class="dragInfo temSpace" colspan="2" rowspan="1">
                                <div class="temCont">
                                    <div class="dragGather" request="template/text">
                                        <div class="temSpace temSpaceCss">
                                            <label class="textLabel">文字</label>
                                            <input type="text" lay-verify="required" placeholder="请输入文本" autocomplete="off" class="layui-input textInput" name="m:demozb:wz" maxlength="50" value="">
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr class="temSpace" request="template/table">
                            <td class="dragInfo temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/date">
                                        <div class="temSpace temSpaceCss">
                                            <label class="dateLabel">开始时间</label>
                                            <input type="text" placeholder="请选择日期" autocomplete="off" class="layui-input dateInput temDate" id="startTime" name="m:demozb:kssj" displaydate="0" format="yyyy-MM-dd" datetype="date" lay-key="1" value="">
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="dragInfo temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/date">
                                        <div class="temSpace temSpaceCss">
                                            <label class="dateLabel">结束时间</label>
                                            <input type="text" placeholder="请选择日期" autocomplete="off" class="layui-input dateInput temDate" id="endTime" name="m:demozb:jssj" displaydate="0" format="yyyy-MM-dd" datetype="date" lay-key="2" value="">
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr class="temSpace" request="template/table">
                            <td class="dragInfo temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/uploadFile">
                                        <div class="temSpace temSpaceCss">
                                            <label class="uploadFileLabel">附件上传</label>
                                            <div class="uploadFileCont" val="">
                                                <button type="button" class="layui-btn uploadFileBtn" filetypes=".doc;.txt;.sql;.xml" isinit="2"> 上传附件 </button>
                                                <input class="layui-upload-file" type="file" accept="" name="file" multiple>
                                                <ul class="uploadFileList" name="m:demozb:fjsz">
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="dragInfo temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/uploadImg">
                                        <div class="temSpace temSpaceCss">
                                            <label class="uploadImgLabel">图片上传</label>
                                            <div class="uploadImgCont" val="">
                                                <button type="button" class="layui-btn uploadImgBtn" isinit="2"> 上传图片 </button>
                                                <input class="layui-upload-file" type="file" accept="" name="file" multiple>
                                                <ul class="uploadImgList" name="m:demozb:tpsz">
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="temCont">
            <div class="dragGather" request="template/table">
                <div class="" style="padding: 10px">
                    <a modetype="2" style="display: none" class="addFormTr layui-btn layui-btn-sm">新增</a>
                    <table class="temTable demozib subFormTable"  border="1" width="100%" cellpadding="10" cellspacing="10">
                        <tbody request="template/table">
                        <tr class="temSpace" request="template/table">
                            <td class="temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                序号
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                子表文字
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                子表开始时间
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                子表结束时间
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                子表附件上传
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                子表图片上传
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subOptState">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                管理
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr class="temSpace subContTem" style="display: none" request="template/table">
                            <td class="temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                0
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/text">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="textLabel">子表文字</label>-->
                                                <input type="text" placeholder="请输入文本" autocomplete="off" class="layui-input textInput" name="s:demozib:zbwz" maxlength="50" value="">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/date">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="dateLabel">子表开始时间</label>-->
                                                <input type="text" placeholder="请选择日期" autocomplete="off" class="layui-input dateInput temDate" name="s:demozib:zbkssj" displaydate="0" format="yyyy-MM-dd" datetype="date" lay-key="3" value="">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/date">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="dateLabel">子表结束时间</label>-->
                                                <input type="text" placeholder="请选择日期" autocomplete="off" class="layui-input dateInput temDate" name="s:demozib:zbjssj" displaydate="0" format="yyyy-MM-dd" datetype="date" lay-key="4" value="">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/uploadFile">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="uploadFileLabel">子表附件上传</label>-->
                                                <div class="uploadFileCont" val="">
                                                    <button type="button" class="layui-btn uploadFileBtn" filetypes=".doc;.txt;.sql;.xml" isinit="2"> 上传附件 </button>
                                                    <input class="layui-upload-file" type="file" accept="" name="file" multiple>
                                                    <ul class="uploadFileList" name="s:demozib:zbfjsz">
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/uploadImg">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="uploadImgLabel">子表图片上传</label>-->
                                                <div class="uploadImgCont" val="">
                                                    <button type="button" class="layui-btn uploadImgBtn" isinit="2"> 上传图片 </button>
                                                    <input class="layui-upload-file" type="file" accept="" name="file" multiple>
                                                    <ul class="uploadImgList" name="s:demozib:zbtpsz">
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div> </td>
                            <td class="temSpace subOptState"><a class="layui-btn layui-btn-sm subDeleteTr">删除</a><a class="layui-btn layui-btn-sm subUpTr">上移</a><a class="layui-btn layui-btn-sm subDownTr">下移</a></td>
                        </tr>
                        <tr class="temSpace" request="template/table">
                            <td class="temSpace">
                                <div class="temCont">
                                    <div class="dragGather" request="template/tag">
                                        <div class="temSpace temSpaceCss">
                                            <div class="temTag">
                                                1
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/text">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="textLabel">子表文字</label>-->
                                                <input type="text" placeholder="请输入文本" autocomplete="off" class="layui-input textInput" name="s:demozib:zbwz" maxlength="50" value="">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/date">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="dateLabel">子表开始时间</label>-->
                                                <input type="text" placeholder="请选择日期" autocomplete="off" class="layui-input dateInput temDate" id="substartTime1" name="s:demozib:zbkssj" displaydate="0" format="yyyy-MM-dd" datetype="date" lay-key="5" value="">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/date">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="dateLabel">子表结束时间</label>-->
                                                <input type="text" placeholder="请选择日期" autocomplete="off" class="layui-input dateInput temDate" id="subendTime1" name="s:demozib:zbjssj" displaydate="0" format="yyyy-MM-dd" datetype="date" lay-key="6" value="">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/uploadFile">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="uploadFileLabel">子表附件上传</label>-->
                                                <div class="uploadFileCont" val="">
                                                    <button type="button" class="layui-btn uploadFileBtn" filetypes=".doc;.txt;.sql;.xml" isinit="2"> 上传附件 </button>
                                                    <input class="layui-upload-file" type="file" accept="" name="file" multiple>
                                                    <ul class="uploadFileList" name="s:demozib:zbfjsz">
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subDialogShowState">
                                <div class="dragSourceInfo">
                                    <div class="temCont">
                                        <div class="dragGather" request="template/uploadImg">
                                            <div class="temSpace temSpaceCss">
                                                <!--<label class="uploadImgLabel">子表图片上传</label>-->
                                                <div class="uploadImgCont" val="">
                                                    <button type="button" class="layui-btn uploadImgBtn" isinit="2"> 上传图片 </button>
                                                    <input class="layui-upload-file" type="file" accept="" name="file" multiple>
                                                    <ul class="uploadImgList" name="s:demozib:zbtpsz">
                                                    </ul>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="temSpace subOptState"><a class="layui-btn layui-btn-sm subDeleteTr">删除</a><a class="layui-btn layui-btn-sm subUpTr">上移</a><a class="layui-btn layui-btn-sm subDownTr">下移</a></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var mainTableId = '${id}';
    layui.config({
        base: serverPath.formPath + staticPath.formPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index', 'setter', 'form', 'layer', 'form','commons','upload','laydate'], function () {
        var $ = layui.$,
            setter = layui.setter,
            layer = layui.layer,
            upload = layui.upload,
            laydate = layui.laydate,
            form = layui.form,
            commons = layui.commons;

        //编辑页面数据回显
        if(mainTableId){
            $.ajax({
                url: serverPath.basePath + '/demo/wDemozb/get',
                type: "POST",
                dataType: 'json',
                data: {"mainTableId":mainTableId},
                success: function (result) {
                    if (commons._filterRecord(result)) {
                        //给主表表单赋值
                        var wDemozbdata= result.data.wDemozb;
                        form.val("formData", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                            "m:demozb:wz": wDemozbdata.wz // "name": "value"
                            ,"m:demozb:kssj": getMyDate(parseInt(wDemozbdata.kssj))
                            ,"m:demozb:jssj": getMyDate(parseInt(wDemozbdata.jssj))
                        });
                        // 回显文件
                        if(wDemozbdata.fjsz){
                            var ZBfjsz = JSON.parse(wDemozbdata.fjsz);
                            $.each(ZBfjsz,function (i,v) {
                                var str = '<li fileid="'+v.fileid+'"><p onclick="downFile(this)">'+v.name+'</p><i class="layui-icon layui-icon-delete deleteFile"></i></li>'

                                $(".tableExcel .uploadFileList").append(str)
                            })
                        }
                        // 回显图片
                        if (wDemozbdata.tpsz){
                            var strimg = '<li fileid="'+JSON.parse(wDemozbdata.tpsz)[0]+'"><div class="fileByImg"><img onclick="downFile(this)" src="' + serverPath.formPath + setter.sysLinkPath + 'attachFile/getFileById?fileId=' + JSON.parse(wDemozbdata.tpsz)[0] + '"></div><i class="layui-icon layui-icon-delete deleteFile"></i></li>'
                            $(".tableExcel .uploadImgList").append(strimg);
                        }
                        //给子表表单赋值
                        var wDemozibListdata= result.data.wDemozibList;
                        $.each(wDemozibListdata,function (i,v) {
                            if(i!=0){
                                $(".addFormTr").click();
                                $('.subFormTable tr').eq(i+2).find('td').eq(0).find('.temTag').html(i+1);
                            }
                            $('.subFormTable tr').eq(i+2).find('input.textInput').val(v.zbwz);
                            var s = getMyDate(parseInt(v.zbkssj))
                            $('.subFormTable tr').eq(i+2).find('td').eq(2).find('input').val(getMyDate(parseInt(v.zbkssj)));
                            $('.subFormTable tr').eq(i+2).find('td').eq(3).find('input').val(getMyDate(parseInt(v.zbjssj)));
                            // 回显子表文件
                            if(wDemozibListdata[i].zbfjsz){
                                var ZBfjsz = JSON.parse(wDemozibListdata[i].zbfjsz);
                                $.each(ZBfjsz,function (index,val) {
                                    var str = '<li fileid="'+val.fileid+'"><p onclick="downFile(this)">'+val.name+'</p><i class="layui-icon layui-icon-delete deleteFile"></i></li>'
                                    $(".subFormTable .uploadFileList").eq(i+1).append(str)
                                })
                            }
                            // 回显子表图片
                            if (wDemozibListdata[i].zbtpsz&&wDemozibListdata[i].zbtpsz!='[]'){
                                var strimg = '<li fileid="'+JSON.parse(wDemozibListdata[i].zbtpsz)[0]+'"><div class="fileByImg"><img onclick="downFile(this)" src="' + serverPath.formPath + setter.sysLinkPath + 'attachFile/getFileById?fileId=' + JSON.parse(wDemozibListdata[i].zbtpsz)[0] + '"></div><i class="layui-icon layui-icon-delete deleteFile"></i></li>'
                                $(".subFormTable .uploadImgList").eq(i+1).append(strimg);
                            }
                        })
                        $('input').attr("disabled","disabled")
                    } else {
                        window.parent.layer.alert(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                }
            });
        }
        //毫秒转成时间
        function getMyDate(str) {
            if (str){
                var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth()+1,
                    oDay = oDate.getDate(),
                    oTime = oYear +'-'+ addZero(oMonth) +'-'+ addZero(oDay) ;
                return oTime;
            }
        }
        //补零操作
        function addZero(num){
            if(parseInt(num) < 10){
                num = '0'+num;
            }
            return num;
        }
        //下载附件
        window.downFile = function (obj){
            var fileId =  $(obj).parents("li").attr("fileId");
            window.location.href = serverPath.formPath + setter.sysLinkPath + "attachFile/download?fileId="+fileId;
        };
        //子表新增行
        $(document).on('click', '.addFormTr', function () {
            var modetype = $(this).attr('modetype'),
                copyObj = $(this).parent().children('.subFormTable'),
                copyHtm = copyObj.find('tr').eq(1);
            if (modetype === '2') {
                // 表格
                copyObj.append('<tr class="temSpace" request="template/table">' + copyHtm.html() + '</tr>');
                var trArr = copyObj.find('tr');
                trArr.eq(trArr.length - 1).find('td').eq(0).find('.temTag').html(trArr.length - 2);
                var _start = trArr.eq(trArr.length - 1).find('td').eq(2).find('input.temDate')[0];
                var _end = trArr.eq(trArr.length - 1).find('td').eq(3).find('input.temDate')[0];
                commons.reflashDate({startName:_start,endName:_end},'',function () {
                    $(_start).click(function () {
                        return false;
                    });
                    $(_end).click(function () {
                        return false;
                    });
                }());
                trArr.eq(trArr.length - 1).find('[isinit="1"]').each(function (i, item) {
                    $(item).attr('isinit', '2');
                });
                if (trArr.length > 3) {
                    trArr.eq(trArr.length - 1).find('[type=radio]').each(function (i, item) {
                        $(item).attr('name', (trArr.eq(1).find('[type=radio]').eq(i).attr('name')) + '_' + (trArr.length - 2))
                    });
                }
            }
        });
        var index = parent.layer.getFrameIndex(window.name);
        $('.return').click(function () {
            parent.layer.close(index);
        });
        //表单中的日期范围控制 引入commons模块调用reflashDate方法。.startTim：开始时间的class，.endTime：结束时间的class
        commons.reflashDate({startName:'#startTime',endName:'#endTime'});
        commons.reflashDate({startName:'#substartTime1',endName:'#subendTime1'});
    })
</script>
</body>
</html>


